window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js', 'imgUpload_module.js'], function(Z, I, M, U, IMG) {
		var user = U.checkLogin();
		var projectVO;
		U.fillHead();
		U.fillFooter();
		initPage();
		var imgs = new Object();
		var um = UM.getEditor('myEditor');
		$(".edui-container").width(568);
		$(".edui-container").css("margin-left", "29px");

		function initPage() { //初始化页面
			U.loadOpt(false);
			U.ajaxget("/project/detail/" + U.getParam("id"), {}, function(json) {
				if(json.success) {
					projectVO = json.models.projectVO;
					if(user.state == 1 && projectVO.userId == user.info.id) {
						$(".project_log").show();
					}
					var povertyObjList = projectVO.povertyObjList;
					$(".help_list").empty();
					$(povertyObjList).each(function() {
						var headImage = "../images/icons/icon_unknow.png";
						if(U.getImg(this.images).length > 0) {
							headImage = U.getImg(this.images)[0];
						}
						var poverty = "<div><div class='head_img'><img src='" + headImage + "'></div><div class='info'><div>" + this.name + "</div><div class='address'><span>住址：</span><span>" + this.address + "</span></div></div><div class='details' id='" + this.id + "'>详情信息</div></div>";
						$(".help_list").append(poverty);
					});
					$(".announcement_details>div:first-child").text(projectVO.name);
					$(".sp-fbt").html(projectVO.title);
					$(".announcement_details>div:nth-child(2)>span:first-child").text("发布时间：" + U.getMyDate(projectVO.gmtCreate, 16));
					//$(".announcement_details>div:nth-child(3)").text(projectVO.intro);
					$(".announcement_details>div:nth-child(5)>span:last-child").text(projectVO.amount + "元");
					$(".announcement_details>div:nth-child(4)>span:nth-child(2)").text(projectVO.povertyName);
					$(".look_unit").attr("id", projectVO.userId);
					$(".look").attr("id", projectVO.responsibleId);
					$(".help_info>div:nth-child(2)>span:nth-child(2)").text(projectVO.responsibleName);
					var jindu = "../images/icons/project_jd_03.png";
					if(projectVO.state == 01) {
						jindu = "../images/icons/project_jd_03.png"; //未开展
					}
					if(projectVO.state == 2 || projectVO.state == 3) {
						jindu = "../images/icons/project_jd_02.png"; //进行中
					}
					if(projectVO.state == 4) {
						jindu = "../images/icons/project_jd_04.png"; //项目结束
					}
					$(".jindu img").attr("src", jindu);
				} else {
					M.toast(false, json.errorMsg);
				}
				/*帮扶计划*/
				U.ajax("/plan/list/" + U.getParam("id"), {}, function(json) {
					if(json.success) {
						$(".stage_list>div").empty();
						console.log(json.models.projectPlanList)
						$(json.models.projectPlanList).each(function(i) {
							var content = "";
							var imgs = "";
							/*if(this.remark == null) {
								if(user.state == 1) {
									if(projectVO.userId == user.info.id) {
										content = "<img class='xiangji' src='../images/icons/icon_xiangji.png'/> <input class='content_text' type='text' />  <span class='sure'>确定</span>";
										imgs = "";
									}
								}
							} else {
								content = this.remark;
								$(this.images.split(",")).each(function() {
									if(this == "") {
										return true;
									}
									imgs += "<div><img class='img' src='" + this + "'></div>";
								});
							}*/
							var plan='<div class="list-item">' +
								'<div class="list-number">' +
									'<span class="item-sp-'+(i+1)+' item-sp-num">'+ (i + 1) +'</span><span>'+this.stepName+'</span>' +
									'<span>' + U.getMyDate(this.beginTime) + "至" + U.getMyDate(this.endTime) +'</span>' +
								'</div>' +
								'<div class="list-last"> '+ this.remark +'</div>' +
								'</div>'
							/*var plan = "<div id='" + this.id + "' class='stage'><div class='s_dot'>" + (i + 1) + "</div><div class='s_title'><div>" + (this.status == "1" ? "已完成" : "未完成") + "</div><div>" + this.stepName + "</div></div><div class='s_content'>" + content + "</div><div class='s_imgs'>" + imgs + "</div><div class='s_time'>" + U.getMyDate(this.beginTime) + "至" + U.getMyDate(this.endTime) + "</div></div>";*/
							$(".stage_list>div").append(plan);
						});
						//add();
					} else {
						M.toast(false, json.errorMsg);
					}
				})
				initEvent();
			});
			/*项目日志*/
			U.ajaxget("/fplogs/byPid/" + U.getParam("id"), {}, function(json) {
				if(json.success) {
					$(".log_list").empty();
					if(json.models.fpLogsList.length == 0) {
						$(".project_log").hide();
					}
					$(json.models.fpLogsList).each(function() {
						/*var img = "../images/icons/icon_unknow.png";
						if(U.getImg(this.headImage).length > 0) {
							img = U.getImg(this.headImage)[0];
						}
						$(".log_list").append("<div><div><div class='l_img'><img src='" + img + "'></div><div class='l_name'>" + this.name + "</div></div><div class='xq'><div class='l_title'>" + this.title + "</div><div class='l_text'>" + this.content + "</div><div class='l_time'>" + U.getMyDate(this.gmtCreate, 16) + "</div></div></div>");*/
						var item="<div style='width: 90%; margin-top: 27px'><div><span style='display: block;width: 10px;height: 10px;background-color: #DEDEDE;float: left;margin-top: 5px'></span><span style='display: block;margin-left: 20px'>"+U.getMyDate(this.gmtCreate)+"</span></div><div style='margin-top: 17px'><span style='clear: left;display: block;margin-left: 20px'>"+this.title+"</span></div><div style='margin-top: 17px'><span style='clear: left;display: block;margin-left: 20px'>"+(this.content==null?'无记录':this.content)+"</span></div></div>"
						$(".log_list").append(item);
					});
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}

		function initEvent() {

			$(".save_btn").click(function() {

				var title = $(".write_log .input input").val();
				var content = UM.getEditor('myEditor').getContent();
				var params = {
					userId: user.info.id,
					projectId: U.getParam("id"),
					title: title,
					content: content

				};
				U.ajax("/fplogs/add", params, function(json) {
					if(json.success) {
						alert("保存成功");
						location.reload();
					} else {
						alert(json.errorMsg);
					}
				});
			});
			$(".look_unit").click(function() {
				U.loadUnit_view(true, {
					"id": $(this).attr("id")
				});
			});
			$(".look").click(function() {
				U.loadduty_view(true, {
					"id": $(this).attr("id")
				});
			});
			$(".help_list .details").unbind("click").click(function() {
				/*U.laodPoor_view(true, {
					"id": $(this).attr("id")
				});*/
				window.location.href="../html/poor_peoleDetail.html?id="+$(this).attr("id");
			});
			$(".l_text").width($("#content>div").width() - 30 * 2 - 60 - 30);
			$("#Complaints_and_reports>div>div").click(function() {
				U.loadComplaint(true, {
					"text": $(this).text()
				});
			});
		}
		IMG.uploadImg({
			'obj': $('#upload_img'),
			'success': function(json, obj) {
				U.loadOpt(false);
				if(json.state == "SUCCESS") {
					var url = json.url;
					$(imgs).append("<div><img class='img' src='" + url + "'> <img class='del' src='../images/icons/icon_shanchu.png'></div>");
					$(".del").unbind("click").click(function() {
						$(this).parent().remove();
					});
				} else {
					M.toast(false, "上传失败！");
				}
			},
			'error': function(msg) {
				M.error(msg);
			}
		});

		function add() {
			$(".xiangji").click(function() {
				imgs = $(this).parent().parent().find(".s_imgs");
				$("#upload_img").click();
			});
			$(".sure").unbind("click").click(function() {
				var Remark = $(this).parent().parent().find(".content_text").val();
				if(Remark == "") {
					M.toast(false, "请输入内容");
					return false;
				}
				var imgs = "";
				$($(this).parent().parent().find(".s_imgs .img")).each(function() {
					if(this == "") {
						return true;
					}
					imgs += "," + $(this).attr("src");
				});
				try {
					imgs = imgs.substring(1);
				} catch(e) {}
				var id = $(this).parent().parent().attr("id");
				var param = {
					id: id,
					images: imgs,
					Remark: Remark
				}
				U.ajax("/plan/update", param, function(json) {
					if(json.success) {
						M.toast(true, "更新完成");
						location.reload();
					} else {
						M.toast(false, json.errorMsg);
					}
				})
			});
		}
	});
}